package m2_work;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");

        Channel c = f.newConnection().createChannel();

        c.queueDeclare("helloworld",true,false,false,null);

        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg = new String(delivery.getBody());
                System.out.println("收到:"+msg);
                for(int i = 0;i < msg.length(); i++){
                    if(msg.charAt(i) == '.'){
                        try {
                            Thread.sleep(1000);
                            System.out.println(msg.charAt(i));
                        }catch (InterruptedException e){}
                    }
                }
                c.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            }
        };


        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        c.basicQos(1);;
        //参数2:true 自动ack确认删除   false:等待回执,成功后删除数据,失败则缓存的数据不删除  消息处理完成后要手动发送回执
        c.basicConsume("helloworld",false,deliverCallback,cancelCallback);
        }
}
